--后台自动检查坐席是否已经签出，为了防止坐席自动关掉了页面，我们监控其有没有关闭网页页面
api = freeswitch.API()
allrow = {}
count = {}
res = {}
local dbh = freeswitch.Dbh("mysql:root:root")
assert(dbh:connected())
function searchOne(tiaojian)
    local function set_session_variables(row)
        for key, val in pairs(row) do
         allrow[key]=val
      end
    end
    local sql_query = tiaojian
    dbh:query(sql_query, set_session_variables)
end
function searchCount(tiaojian)
    local function set_session_variables(row)
        for key, val in pairs(row) do
         count = val
      end
    end
    local sql_query = tiaojian
    dbh:query(sql_query, set_session_variables)
end
function execute(tiaojian)
    local function set_session_variables(row)
        for key, val in pairs(row) do
         res = val
      end
    end
    local sql_query = tiaojian
    dbh:query(sql_query, set_session_variables)
end
xunhuan = true
searchOne("select * from cti_zuoxi_login_flag where zuoxi="..argv[1])
if (allrow['first']~='0') then--表示有一个已经正在循环了,停止当前的循环
    execute("UPDATE cti_zuoxi_login_flag SET first=0 where zuoxi="..argv[1])
    while xunhuan do
        freeswitch.msleep(28000)--与以上的60参数可以互相协调使用
        --print("xunhuan")
        searchOne("select * from cti_zuoxi_login_flag where zuoxi="..argv[1])
        searchCount("select count(*) from cti_zuoxi_login_flag where zuoxi="..argv[1])
        if (os.time()-(allrow['time']-allrow['diff'])>60) then --查出来了 ，(allrow['time']-allrow['diff'])为客户的时间，相对于本地服务器时间相差diff
            api:executeString("callcenter_config agent set status "..argv[1].."@${domain} 'Logged Out'");
            xunhuan = false
            break
        end
        --print("xunhuan-wan")
    end
    execute("UPDATE cti_zuoxi_login_flag SET first=1 where zuoxi="..argv[1])
    api:executeString("callcenter_config agent set status "..argv[1].."@${domain} 'Logged Out'");
end
print(argv[1].."---------check_zuoxi_tuichu----------\n")
